﻿package mortal.game.view.npc.base
{
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import flash.display.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.core.*;
    import frEngine.effectEditTool.temple.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.model.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.mvc.interfaces.*;

    public class NpcTaskDialogModuleBase extends NpcWindow
    {
        protected var _roleModel:RoleModelPlayer;
        protected var _img2d:Img2D;
        protected var _npcInfo:NPCInfo;
        protected var _bgBitmapData:BitmapData;
        protected var _isResGot:Boolean = false;
        protected var _npc3d:Rect3DObject;
        protected var _rectMask:Rect3DMask;
        protected var _effectPlayerArray:Vector.<EffectPlayer>;
        protected var _bodyPlayer:ActionPlayer;
        protected var _hangPlayer:ActionPlayer;

        public function NpcTaskDialogModuleBase(param1:ILayer = null)
        {
            this._effectPlayerArray = new Vector.<EffectPlayer>;
            super(param1);
            return;
        }// end function

        protected function addSelf() : void
        {
            if (!this._isResGot)
            {
                return;
            }
            this._npc3d = Rect3DManager.instance.registerWindow(false, new Rectangle(0, 0, this._img2d.width - 5, this._img2d.height - 6), this);
            this._rectMask = this._npc3d.createRect3dMask(new Rectangle(-300, -300, 500 + 300, this._img2d.height - 6 + 300));
            this._npc3d.addImg(this._img2d);
            if (this._roleModel == null)
            {
                this._roleModel = FrObjectPool.getObject(RoleModelPlayer, this);
                this._roleModel.entityInfo = Cache.instance.role.roleEntityInfo;
                this._roleModel.scaleAll = 2.7;
                this._roleModel.setRenderList(this._npc3d.renderList);
            }
            this._npc3d.addObject3d(this._roleModel.bodyPlayer, 120, 390, true);
            if (this._bodyPlayer)
            {
                this._npc3d.disposeObj3d(this._bodyPlayer);
                this._bodyPlayer = null;
            }
            return;
        }// end function

        protected function addNpc3d() : void
        {
            stageResize();
            if (this._npcInfo == null || !DisplayUtil.isBitmapDataUsable(this._bgBitmapData))
            {
                return;
            }
            this._npc3d = Rect3DManager.instance.getRect3dByWindow(this);
            if (!this._npc3d)
            {
                this._npc3d = Rect3DManager.instance.registerWindow(false, new Rectangle(0, 0, this._img2d.width - 5, this._img2d.height - 6), this);
                this._rectMask = this._npc3d.createRect3dMask(new Rectangle(-300, -300, 500 + 300, this._img2d.height - 6 + 300));
            }
            Rect3DManager.instance.windowShowHander(null, this);
            this._npc3d.addImg(this._img2d);
            this.updateToCurNpc();
            return;
        }// end function

        protected function updateToCurNpc() : void
        {
            var _loc_1:Array = null;
            var _loc_2:DefInfo = null;
            var _loc_3:Vector.<Array> = null;
            var _loc_4:int = 0;
            var _loc_5:Array = null;
            var _loc_6:Array = null;
            var _loc_7:EffectPlayer = null;
            var _loc_8:Array = null;
            var _loc_9:Rect3DMask = null;
            if (this._npc3d && this._npcInfo)
            {
                if (this._bodyPlayer == null)
                {
                    this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
                }
                if (this._bodyPlayer.meshUrl != this._npcInfo.tnpc.mesh && this._bodyPlayer.animUrl != this._npcInfo.tnpc.bone)
                {
                    this._bodyPlayer.load(this._npcInfo.tnpc.mesh, this._npcInfo.tnpc.bone, this._npcInfo.tnpc.texture, this._npc3d.renderList, 0.1, EBlendType.BLEND_ALPHA0);
                    if (this._npcInfo.tnpc.hangModel)
                    {
                        _loc_1 = this._npcInfo.tnpc.hangModel.split("#");
                        if (_loc_1.length >= 4)
                        {
                            if (!this._hangPlayer)
                            {
                                this._hangPlayer = FrObjectPool.getObject(ActionPlayer, this);
                                this._hangPlayer.changeAction(ActionName.Stand);
                                this._hangPlayer.selectEnabled = true;
                                this._hangPlayer.timerContorler = TimeControler.defaultTimeControler;
                                this._hangPlayer.play();
                            }
                            this._hangPlayer.load(_loc_1[0], _loc_1[1], _loc_1[2], this._npc3d.renderList, 0.1, EBlendType.BLEND_ALPHA0);
                            this._hangPlayer.hangBoneName = _loc_1[3];
                            this._bodyPlayer.hang(this._hangPlayer);
                        }
                    }
                    if (this._npcInfo.tnpc.selfEffect)
                    {
                        _loc_3 = GameStrUtil.alzToArray(this._npcInfo.tnpc.selfEffect);
                        _loc_5 = _loc_3[0];
                        _loc_6 = _loc_3[1];
                        _loc_4 = 0;
                        while (_loc_4 < _loc_5.length)
                        {
                            
                            if (this._effectPlayerArray.length > _loc_4)
                            {
                                _loc_7 = this._effectPlayerArray[_loc_4];
                                this._bodyPlayer.unHang(_loc_7);
                                _loc_7.dispose();
                            }
                            _loc_7 = EffectPlayerPool.instance.getEffectPlayer(_loc_5[_loc_4], null, this._npc3d.renderList, false);
                            this._effectPlayerArray[_loc_4] = _loc_7;
                            if (_loc_6.length > _loc_4)
                            {
                                _loc_7.hangBoneName = _loc_6[_loc_4];
                            }
                            if (_loc_7.temple is TempleRole)
                            {
                                (_loc_7.temple as TempleRole).setRoleParams(this._bodyPlayer, null, null);
                            }
                            else
                            {
                                this._bodyPlayer.hang(_loc_7);
                            }
                            _loc_7.play(PlayMode.ANIMATION_LOOP_MODE);
                            _loc_4++;
                        }
                    }
                }
                this._bodyPlayer.changeAction(ActionName.Stand);
                this._bodyPlayer.play();
                _loc_2 = GameDefConfig.instance.getNpcMaxScale(this._npcInfo.tnpc.code);
                if (_loc_2 != null)
                {
                    _loc_8 = _loc_2.text.split(",");
                    this._bodyPlayer.scaleValue = parseFloat(_loc_8[2]);
                    _loc_9 = _loc_2.value == "1" ? (this._rectMask) : (null);
                    this._npc3d.addObject3d(this._bodyPlayer, 120 + int(_loc_8[0]), 390 + int(_loc_8[1]), true, _loc_9, false);
                }
                else
                {
                    this._bodyPlayer.scaleValue = 2.8;
                    this._npc3d.addObject3d(this._bodyPlayer, 120, 390, true, null, false);
                }
                if (this._roleModel && this._roleModel.bodyPlayer)
                {
                    this._npc3d.disposeObj3d(this._roleModel.bodyPlayer);
                }
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this._hangPlayer)
            {
                this._bodyPlayer.unHang(this._hangPlayer);
                this._hangPlayer.dispose();
                this._hangPlayer = null;
            }
            if (this._bodyPlayer != null)
            {
                this._effectPlayerArray.length = 0;
                this._bodyPlayer.dispose();
                this._bodyPlayer = null;
            }
            if (this._roleModel)
            {
                this._roleModel.dispose(param1);
                this._roleModel = null;
            }
            if (this._npc3d)
            {
                this._npc3d.clearRect3dMask(this._rectMask);
                this._rectMask = null;
                Rect3DManager.instance.disposeRect3d(this._npc3d);
                this._npc3d = null;
            }
            return;
        }// end function

    }
}
